package com.projetTDA.tournoi_encours;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

import com.projetTDA.R;
import com.projetTDA.bdd.DataAccessLayer;
import com.projetTDA.metier.Equipe;
import com.projetTDA.metier.Match;
import com.projetTDA.metier.Tournoi;


//Fonction qui permet de renseigner les scores lors d'un tournoi en mode eliminatoire
public class TournoiEncoursEliminatoire extends Activity {


	private TableLayout tl_eliminatoire = null;
	private Tournoi mTournoisEncours = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tournois_encours_eliminatoire);

		int id_tournoi = getIntent().getIntExtra("ID_TOURNOI", -1);
		System.out.println("Ceci est l'id tournoi"+id_tournoi);
		DataAccessLayer myDAL = new DataAccessLayer(this);
		for(Tournoi t : myDAL.getListeTournoi())
		{
			if(id_tournoi == t.getId_tournoi())
			{
				mTournoisEncours = t;
			}
		}

		String textRang1 = "";
		String textRang2 = "";
		String textRang3 = "";

		switch (mTournoisEncours.getClassement().keySet().size()) {
		case 2:
			textRang1 = "Finale :\n";
			break;

		case 4:
			textRang1 = "Demi finales :\n";
			textRang2 = "\nFinale :\n";

		case 8:
			textRang1 = "Quart de finales :\n";
			textRang2 = "\nDemi finales :\n";
			textRang3 = "\nFinale :\n";
			break;
		}

		tl_eliminatoire = (TableLayout) findViewById(R.id.tl_eliminatoire);

		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);

		boolean tournoiFini = true;

		boolean stringRang1 = true;
		boolean stringRang2 = true;
		boolean stringRang3 = true;

		for(Match m : mTournoisEncours.getListe_matchs())
		{
			if(m.getScore1() == null || m.getScore2() == null)
			{
				tournoiFini = false;
			}

			System.out.println(m);
			if (m.getRang() == 1)
			{	
				if (stringRang1){
					TableRow tableRow = new TableRow(this); 
					tl_eliminatoire.addView(tableRow, layoutParams);
					tableRow.addView(generateTextView(textRang1, layoutParams, true));
					stringRang1 = false;
				}

				TableRow tableRow2 = new TableRow(this); 
				tl_eliminatoire.addView(tableRow2, layoutParams);
				tableRow2.addView(generateTextView(m.getEquipe1().getNom_equipe() + " (" + m.getScore1() +") Vs " + m.getEquipe2().getNom_equipe() + " (" + m.getScore2() + ")", layoutParams, false));

				/*tr_eliminatoire = new TableRow(this);
				// On affiche qu'une seule fois "Rang 1"
				if (stringRang1){
					sl_eliminatoire.addView(generateTextView("Rang 1 :\n", layoutParams));
				}
				stringRang1 = false;

				tr_eliminatoire.addView(generateTextView(m.getEquipe1().getNom_equipe() + " (" + m.getScore1() +") Vs " + m.getEquipe2().getNom_equipe() + " (" + m.getScore2() + ")", layoutParams));
				tl_eliminatoire.addView(tr_eliminatoire, layoutParams);
				//tl_eliminatoire.removeAllViews();*/
			}
			if (m.getRang() == 2)
			{
				if (stringRang2){
					TableRow tableRow = new TableRow(this); 
					tl_eliminatoire.addView(tableRow, layoutParams);
					tableRow.addView(generateTextView(textRang2, layoutParams, true));
					stringRang2 = false;
				}

				TableRow tableRow2 = new TableRow(this); 
				tl_eliminatoire.addView(tableRow2, layoutParams);
				tableRow2.addView(generateTextView(m.getEquipe1().getNom_equipe() + " (" + m.getScore1() +") Vs " + m.getEquipe2().getNom_equipe() + " (" + m.getScore2() + ")", layoutParams, false));
			}

			if (m.getRang() == 3)
			{
				if (stringRang3){
					TableRow tableRow = new TableRow(this); 
					tl_eliminatoire.addView(tableRow, layoutParams);
					tableRow.addView(generateTextView(textRang3, layoutParams, true));
					stringRang3 = false;
				}

				TableRow tableRow2 = new TableRow(this); 
				tl_eliminatoire.addView(tableRow2, layoutParams);
				tableRow2.addView(generateTextView(m.getEquipe1().getNom_equipe() + " (" + m.getScore1() +") Vs " + m.getEquipe2().getNom_equipe() + " (" + m.getScore2() + ")", layoutParams, false));

			}
		}

		String nomVainqueur = "TBD";

		if(tournoiFini)
		{
			mTournoisEncours.trierClassement();
			nomVainqueur = ((Equipe) mTournoisEncours.getClassement().keySet().toArray()[0]).getNom_equipe();
		}

		TableRow tableRow = new TableRow(this);
		tl_eliminatoire.addView(tableRow, layoutParams);

		LinearLayout linLayout = new LinearLayout(this);
		linLayout.setOrientation(LinearLayout.HORIZONTAL);
		linLayout.setPadding(0, 10, 0, 0);

		ImageView iv = new ImageView(this);
		iv.setImageResource(R.drawable.kourhone);
		iv.setPadding(0, 0, 5, 0);

		linLayout.addView(iv);
		linLayout.addView(generateTextView("Vainqueur : " + nomVainqueur, layoutParams, true));

		tableRow.addView(linLayout, layoutParams);

	}

	public TextView generateTextView(String texte, LayoutParams ly, Boolean bold) {
		TextView result = new TextView(this);
		result.setTextColor(Color.BLACK);
		result.setText(texte);
		result.setTextSize(15);
		if(bold)
		{
			result.setTypeface(null, Typeface.BOLD);
		}
		result.setVisibility(View.VISIBLE);
		result.setLayoutParams(ly);
		return result;
	}


}
